

package com.yundingai.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yundingai.domain.entity.AllPlan;
import com.yundingai.domain.entity.RelUserPlan;
import com.yundingai.exception.BusinessException;
import com.yundingai.mapper.RelUserPlanMapper;
import com.yundingai.service.AllPlanService;
import com.yundingai.mapper.AllPlanMapper;
import com.yundingai.vm.enums.ResultStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author liyuxiang
 * @description 针对表【all_plan】的数据库操作Service实现
 * @createDate 2025-07-16 12:43:19
 */
@Service
public class AllPlanServiceImpl extends ServiceImpl<AllPlanMapper, AllPlan>
        implements AllPlanService{
    @Autowired
    AllPlanMapper allPlanMapper;
    @Autowired
    RelUserPlanMapper relUserPlanMapper;
    @Override
    public List<AllPlan> allPlan() {
        List<AllPlan> allPlans = allPlanMapper.selectList(null);
        return allPlans;
    }

    //查询用户所有未删除计划
    @Override
    public List<AllPlan> selectPlansByUserId(Integer userId) {
        List<AllPlan> allPlans = allPlanMapper.selectPlansByUserId(userId);
        return allPlans;
    }

    //更新计划
    @Override
    public void updatePlan(AllPlan plan) {
        plan.setUpdateTime(new Date());
        allPlanMapper.updatePlan(plan);
    }

    //删除计划
    @Override
    public void deletePlan(Integer planId) {
        allPlanMapper.deletePlan(planId);
        relUserPlanMapper.deleteRelUserPlan(planId);
    }

    //新增计划
    @Override
    @Transactional
    public void addPlan(AllPlan plan, Integer userId) {
        //将planContent转json
        String jsonString = JSON.toJSONString(plan.getPlanContent());
        plan.setPlanContent(jsonString);
        plan.setCreateTime(new Date());
        allPlanMapper.addPlan(plan);
        relUserPlanMapper.insertRelUserPlan(plan.getPlanId(),userId);
    }

    //获取单个计划
    @Override
    public AllPlan selectPlanById(Integer planId) {
        AllPlan plan = allPlanMapper.selectPlanById(planId);
        return plan;
    }

    @Override
    public int deleteByPlanName(String planName,String userId) {
        int count = allPlanMapper.selectAllPlanByPlanNameAndUserId(planName, Integer.parseInt(userId));
        if (count == 0) {
            throw new BusinessException(ResultStatusEnum.PLAN_NOT_FOUND);
        }
        //删除计划
        allPlanMapper.delete(new QueryWrapper<AllPlan>().lambda().eq(AllPlan::getPlanName, planName));
        return relUserPlanMapper.delete(new QueryWrapper<RelUserPlan>().lambda().eq(RelUserPlan::getUserId, userId));
    }


}